
#include "FreeRTOS.h"
#include "task.h"
#include "stm32f0xx.h"
#include "semphr.h"

#define  BSP_SPI_MOD
#include "bsp_spi.h"


tSPI bsp_spi;




static void bsp_spi_init_nss(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(BSP_SPI1_NSS_GPIO_Port, BSP_SPI1_NSS_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin : BSP_SPI1_NSS_Pin */
  GPIO_InitStruct.Pin = BSP_SPI1_NSS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(BSP_SPI1_NSS_GPIO_Port, &GPIO_InitStruct);

}


void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hspi->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */

  /* USER CODE END SPI1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**SPI1 GPIO Configuration
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* SPI1 interrupt Init */
    HAL_NVIC_SetPriority(SPI1_IRQn, 3, 0);
    HAL_NVIC_EnableIRQ(SPI1_IRQn);
  /* USER CODE BEGIN SPI1_MspInit 1 */

  /* USER CODE END SPI1_MspInit 1 */
  }

}



static void bsp_spi_init_spi(tSPI *spi)
{
  spi->hspi1.Instance = SPI1;
  spi->hspi1.Init.Mode = SPI_MODE_MASTER;
  spi->hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  spi->hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  spi->hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  spi->hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  spi->hspi1.Init.NSS = SPI_NSS_SOFT;
  spi->hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  spi->hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  spi->hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  spi->hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  spi->hspi1.Init.CRCPolynomial = 7;
  spi->hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  spi->hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  if (HAL_SPI_Init(&spi->hspi1) != HAL_OK)
  {

  }
}


HAL_StatusTypeDef bsp_spi_lock_init(tSPI *spi)
{
	spi->lock = xSemaphoreCreateBinary();

	if (spi->lock == NULL)
	{
		return HAL_ERROR;
	}
	
	xSemaphoreGive(spi->lock);
	
	return HAL_OK;
}


BaseType_t bsp_spi_lock(void)
{
  BaseType_t xReturn = pdTRUE;

  xReturn = xSemaphoreTake(bsp_spi.lock, portMAX_DELAY);
  if (pdTRUE == xReturn)
	{

	}
	else
	{
	
	}
	
	
	return xReturn;
}



void bsp_spi_unlock(void)
{
	xSemaphoreGive(bsp_spi.lock);
}



HAL_StatusTypeDef bsp_spi_init(tSPI *spi)
{
	HAL_StatusTypeDef res = HAL_OK;
	
	
	res = bsp_spi_lock_init(spi);
	if (res != HAL_OK)
	{
		return res;
	}
	
	bsp_spi_init_nss();
	bsp_spi_init_spi(spi);
	
	return res;
}




















